package com.qcxk.yuqian.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qcxk.yuqian.system.entity.User;
import com.qcxk.yuqian.system.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qcxk.yuqian.system.entity.Dept;
import com.qcxk.yuqian.system.utils.RedisUtil;

import com.alibaba.fastjson.JSONObject;
import com.qcxk.yuqian.system.service.IDeptService;
import com.qcxk.yuqian.system.component.behaviour.BehaviourOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcxk.yuqian.system.component.constant.SystemConstant;

import javax.servlet.http.HttpServletRequest;

import java.lang.reflect.Field;
import java.util.List;

/**
 * @auther lp
 * @create 2021-05-24 18:35:50
 * @describe 部门表前端控制器
 */
@RestController
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private IDeptService deptService;
    @Autowired
    private UserService userService;

    @BehaviourOperator(desc = "查询部门树")
    @RequestMapping(value = "/getDept")
    public JSONObject loadDept(Dept dept, String isContainUser) {
        JSONObject json = new JSONObject();
        if (dept == null || dept.getId() == null) {
            dept = new Dept();
            dept.setId(1);
        }
        List<Dept> depts = deptService.getChildList("" + dept.getId(), isContainUser);
        if (depts != null) {
            json.put("code", "0");
            json.put("data", depts);
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "查询Dept列表")
    @RequestMapping(value = "/queryDeptList")
    public JSONObject queryDeptList(Page page, Dept dept) {
        JSONObject json = new JSONObject();
        page = deptService.queryDeptPage(page, dept);
        json.put("code", "0");
        json.put("current", page.getCurrent());
        json.put("size", page.getSize());
        json.put("total", page.getTotal());

        List<Dept> list = page.getRecords();

        json.put("dataList", list);
        return json;
    }

    @BehaviourOperator(desc = "单一属性修改Dept")
    @RequestMapping(value = "/updateDeptSingle")
    public JSONObject updateDeptSingle(Integer id, String column, String value) throws NoSuchFieldException, IllegalAccessException {
        JSONObject json = new JSONObject();
        Dept dept = deptService.getById(id);
        Class<? extends Dept> clazz = dept.getClass();// 获取到对象对应的class对象
        Field nameField = clazz.getDeclaredField(column);// 获取私有成员变量:column
        nameField.setAccessible(true);// 设置操作权限为true
        nameField.set(dept, value);
        if (deptService.updateById(dept)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "整体修改Dept")
    @RequestMapping(value = "/updateDeptMultiple")
    public JSONObject updateDeptMultiple(Dept dept) {
        JSONObject json = new JSONObject();

        if (deptService.updateById(dept)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "删除Dept")
    @RequestMapping(value = "/deleteDeptById")
    public JSONObject deleteDeptById(Dept dept) {
        JSONObject json = new JSONObject();
        dept = deptService.getById(dept.getId());
        if (dept == null) {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DEPTNOTEX_ERROR_CN);
            return json;
        }
        //解挂下级部门
        QueryWrapper<Dept> deptQueryWrapper = new QueryWrapper<>();
        deptQueryWrapper.eq("parent_id", dept.getId());
        List<Dept> deptList = deptService.list(deptQueryWrapper);
        if (deptList != null && !deptList.isEmpty()) {
            Dept finalDept = dept;
            deptList.stream().forEach(e -> {
                e.setParentId(finalDept.getParentId());
            });
            deptService.updateBatchById(deptList);
        }
        //解挂用户
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("dept_id", dept.getId());
        List<User> userList = userService.list(userQueryWrapper);
        if (userList != null && !userList.isEmpty()) {
            Dept finalDept = dept;
            userList.stream().forEach(e -> {
                e.setDeptId(finalDept.getParentId());
            });
            userService.updateBatchById(userList);
        }
        if (deptService.deleteDeptById(dept)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "新增Dept")
    @RequestMapping(value = "/addDept")
    public JSONObject addDept(Dept dept) {
        JSONObject json = new JSONObject();
        if (deptService.save(dept)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

}

